package swind.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import swind.web.entities.ExamUser;
import swind.web.service.ILoginService;
import swind.web.service.IStudentService;
import swind.web.service.IVoMachine;
import swind.web.vo.LoginUserVo;
import swind.web.vo.SubjectVo;
import swind.web.vo.TestVo;
import swind.web.vo.UserAnswerVo;

@Controller
public class StudentSubjectController {
	
	@Resource(name="studentServiceImp")
	IStudentService studentServiceImp;
	
	@Resource(name="loginServiceImp")
	ILoginService loginServiceImp;
	
	@Resource(name="voMachine")
	IVoMachine voMachine;
	
	@RequestMapping(value="stu/subshow")
	public String subshow(Model model,HttpSession session){
		
		ExamUser eu=loginServiceImp.getUserByName(voMachine.VoToEntity((LoginUserVo) session.getAttribute("user")));
		List<SubjectVo> sev=studentServiceImp.dealSubjectScoreByExamUser(eu,studentServiceImp.getSubjectByExamUser(eu),studentServiceImp.getScoreByExamUser(eu));
		model.addAttribute("subjects",sev);
		
		return "mainPages/student/subjectTables";
	}
	
	
	@ResponseBody
	@RequestMapping(value="stu/showajax",method=RequestMethod.POST)
	public SubjectVo showajax(String userid,String subId){
		System.out.println("showajax:"+userid);
		ExamUser eu=studentServiceImp.getExamUserById(userid);
		List<SubjectVo> sev=studentServiceImp.dealSubjectScoreByExamUser(eu,studentServiceImp.getSubjectByExamUser(eu),studentServiceImp.getScoreByExamUser(eu));
		SubjectVo sv=null;
		for(SubjectVo svt:sev){
			if(svt.getScoreVo()!=null){
				if(svt.getScoreVo().getSubjectId()==Integer.valueOf(subId)){
					sv=svt;
				}
			}
		}
		
		return sv;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="stu/showTest")
	public String testshow(HttpServletRequest request,String subId){
		//把单次考试数据放入application作用域中
		
		HttpSession session = request.getSession();
		LoginUserVo userVo= (LoginUserVo)session.getAttribute("user");
		
		if(studentServiceImp.hasScore(Integer.valueOf(subId), userVo.getUserId())){
			return "error/error500";
		}
		
		ServletContext application = request.getSession().getServletContext();
		Map<UserAnswerVo,List<TestVo>> userTestMap = null;
		List<TestVo> testList = null;
		UserAnswerVo userAnswerVo = new UserAnswerVo();
		userAnswerVo.setLoginUserVo(userVo);
		userAnswerVo.setSubId(Integer.valueOf(subId));
		userAnswerVo.setExamTime(studentServiceImp.getExamTime(Integer.valueOf(subId)));
		
		if(application.getAttribute("userTestMap")!=null){
			userTestMap = (Map<UserAnswerVo, List<TestVo>>) application.getAttribute("userTestMap");
			if(userTestMap.keySet().contains(userAnswerVo)){
				
				Iterator<UserAnswerVo> userAnswerVoIte= userTestMap.keySet().iterator();
				while(userAnswerVoIte.hasNext()){
					UserAnswerVo temp = userAnswerVoIte.next();
					if(temp.getLoginUserVo().equals(userAnswerVo.getLoginUserVo())){
						userAnswerVo = temp;
					}
				}
				
				testList = userTestMap.get(userAnswerVo);
				request.setAttribute("answerMap", userAnswerVo.getAnswerMap());
				request.setAttribute("answerMapMulti", userAnswerVo.getAnswerMapMulti());
				request.setAttribute("testList", testList);
				request.setAttribute("beginTime", userAnswerVo.getBeginTime());
				request.setAttribute("examTime", userAnswerVo.getExamTime());
				
				return "mainPages/student/testPage";
			}//else{不包含直接去下面生成题目}
				
		}else if(application.getAttribute("userTestMap")==null){
			userTestMap = new HashMap<UserAnswerVo,List<TestVo>>();
		}
		
		//生成题目并放入学生考题映射表中
		testList = studentServiceImp.getTestBySubject(Integer.valueOf(subId));
		if(testList == null){
			return "error/error500";
		}
		userAnswerVo.setBeginTime(new Date());
		userTestMap.put(userAnswerVo, testList);
		application.setAttribute("userTestMap", userTestMap);
		request.setAttribute("testList", testList);
		request.setAttribute("beginTime", userAnswerVo.getBeginTime());
		request.setAttribute("examTime", userAnswerVo.getExamTime());
		
		return "mainPages/student/testPage";
	}
	
	
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value="stu/saveTest",method=RequestMethod.POST)
	public boolean saveTest(String[] idArr,String[] answerArr,String[] idArrMulti,String[] answerArrMulti,HttpServletRequest request){
		
		
		LoginUserVo user = (LoginUserVo) request.getSession().getAttribute("user");
		UserAnswerVo temp = new UserAnswerVo();
		temp.setLoginUserVo(user);
		UserAnswerVo userAnswerVo = null;
		Map<UserAnswerVo,List<TestVo>> userTestMap = (Map<UserAnswerVo, List<TestVo>>) request.getSession().getServletContext().getAttribute("userTestMap");
		Iterator<UserAnswerVo> userAnswerVoIte= userTestMap.keySet().iterator();
		while(userAnswerVoIte.hasNext()){
			UserAnswerVo temp2 = userAnswerVoIte.next();
			if(temp2.getLoginUserVo().equals(temp.getLoginUserVo())){
				userAnswerVo = temp2;
			}
		}
		if(idArr!=null){
			for(int i = 0;i<idArr.length;i++){
				userAnswerVo.getAnswerMap().put(idArr[i], answerArr[i]);
			}
		}
		if(idArrMulti!=null){
			for(int i = 0;i<idArrMulti.length;i++){
				userAnswerVo.getAnswerMapMulti().put(idArrMulti[i], answerArrMulti[i]);
			}
		}
		
		return true;
	}
	
	@ResponseBody
	@SuppressWarnings("unchecked")
	@RequestMapping(value="stu/submitTest",method=RequestMethod.POST)
	public boolean submitTest(HttpServletRequest request,String[] idArr,String[] idArrMulti,String[] answerArrMulti,String[] answerArr){

		/*if(idArr.length==0&&idArrMulti.length==0){
			return false;
		}
		*/
		LoginUserVo student= (LoginUserVo)request.getSession().getAttribute("user");
		Map<UserAnswerVo, List<TestVo>> userTestMap = (Map<UserAnswerVo, List<TestVo>>) request.getSession().getServletContext().getAttribute("userTestMap");
		UserAnswerVo temp = new UserAnswerVo();
		temp.setLoginUserVo(student);
		UserAnswerVo userAnswerVo = null;
		Iterator<UserAnswerVo> userAnswerVoIte= userTestMap.keySet().iterator();
		while(userAnswerVoIte.hasNext()){
			UserAnswerVo temp2 = userAnswerVoIte.next();
			if(temp2.getLoginUserVo().equals(temp.getLoginUserVo())){
				userAnswerVo = temp2;
			}
		}
		
		int subId = userAnswerVo.getSubId();
		
		int singleTotalSocre = 0 ; 
		int multiTotalSocre = 0; 
		if(idArr!=null){
			singleTotalSocre = studentServiceImp.countTestScore(idArr,answerArr);
		}
		if(idArrMulti!=null){
			multiTotalSocre = studentServiceImp.countTestScore(idArrMulti,answerArrMulti);
		}
		studentServiceImp.setNewScore(singleTotalSocre+multiTotalSocre,subId,student.getUserId());
		//计算完成后清除application作用域的数据
		userTestMap.remove(userAnswerVo);
		
		return true;
	}
}
